package org.example.everyday;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author anlu
 * @date 2022/2/24 17:04
 */
public class Lc1706 {

    public static void main(String[] args) {
        Lc1706 lc1706 = new Lc1706();
        int[][] grid = {{1,1,1,-1,-1},
                        {1,1,1,-1,-1},
                        {-1,-1,-1,1,1},
                        {1,1,1,1,-1},
                        {-1,-1,-1,-1,-1}};
        int[] result = lc1706.findBall(grid);
        for (int num : result){
            System.out.println(num);
        }
    }

    public int[] findBall(int[][] grid) {
        int n = grid[0].length;
        int[] ans = new int[n];
        for (int j = 0; j < n; j++) {
            //球的初始列
            int col = j;
            for (int[] row : grid){
                int dir = row[col];
                //移动球
                col += dir;
                //到达侧边或者v形状
                if(col < 0 || col == n || row[col] != dir){
                    col = -1;
                    break;
                }
            }
            //col >= 0为成功到达底部
            ans[j] = col;
        }
        return ans;
    }
}
